package com.himiotech.core.project.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.himiotech.core.project.data.mapper.DictDataMapper;
import com.himiotech.core.project.helper.utils.DictUtils;
import com.himiotech.core.project.model.entity.dict.DictData;
import com.himiotech.core.project.model.metadata.DictDataMetadata;
import com.himiotech.core.project.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements IDictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 查询分页数据
     *
     * @param page     分页信息
     * @param dictData 业务信息查询条件
     * @return 分页对象及数据
     */
    @Override
    public Page<DictData> selectDictDataListPage(Page<DictData> page, DictData dictData) {
        QueryWrapper<DictData> query = Wrappers.query();
        query.like(ObjectUtil.isNotEmpty(dictData.getDictLabel()), DictDataMetadata.DICT_LABEL.getColName(), dictData.getDictLabel());
        query.eq(ObjectUtil.isNotEmpty(dictData.getDictType()), DictDataMetadata.DICT_TYPE.getColName(), dictData.getDictType());
        query.eq(ObjectUtil.isNotEmpty(dictData.getDictValue()), DictDataMetadata.DICT_VALUE.getColName(), dictData.getDictValue());
        query.eq(ObjectUtil.isNotEmpty(dictData.getStatus()), DictDataMetadata.STATUS.getColName(), dictData.getStatus());
        page = dictDataMapper.selectPage(page, query);
        return page;
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        QueryWrapper<DictData> query = Wrappers.<DictData>query()
                .eq(DictDataMetadata.DICT_TYPE.getColName(), dictType)
                .eq(DictDataMetadata.DICT_VALUE.getColName(), dictValue);
        DictData dictData = dictDataMapper.selectOne(query);
        if (dictData != null) {
            return dictData.getDictLabel();
        }
        return null;
    }

    /**
     * 批量删除字典数据
     *
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public void deleteDictDataByIds(String ids) {
        List<Long> dictCodes = Arrays.stream(ids.split(","))
                .map(Convert::toLong).collect(Collectors.toList());
        for (Long dictCode : dictCodes) {
            DictData data = dictDataMapper.selectById(dictCode);
            dictDataMapper.deleteById(dictCode);
            List<DictData> dictDataList = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDataList);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(DictData dictData) {
        int row = dictDataMapper.insert(dictData);
        if (row > 0) {
            List<DictData> dictDataList = dictDataMapper.selectDictDataByType(dictData.getDictType());
            DictUtils.setDictCache(dictData.getDictType(), dictDataList);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(DictData dictData) {
        int row = dictDataMapper.updateById(dictData);
        if (row > 0) {
            List<DictData> dictDataList = dictDataMapper.selectDictDataByType(dictData.getDictType());
            DictUtils.setDictCache(dictData.getDictType(), dictDataList);
        }
        return row;
    }
}
